package com.focusai.efairy.ui.adapter.dev;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.focusai.efairy.R;
import com.focusai.efairy.model.report.DevLineData;
import com.focusai.efairy.ui.activity.dev.DeviceDetailInfoActivity;
import com.focusai.efairy.ui.adapter.DevDetailChartAdapter;
import com.focusai.efairy.ui.widget.LineMarkerView;
import com.focusai.efairy.utils.DateUtils;
import com.focusai.efairy.utils.PrUtils;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.LimitLine;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;

import me.drakeet.multitype.ItemViewBinder;

/**
 * Created by Luogw on 2017/8/22.
 */

public class DeviceInfoReportViewBinder extends ItemViewBinder<DevLineData, DeviceInfoReportViewBinder.ViewHolder> {

    public static final String TAG = DevDetailChartAdapter.class.getName();
    private int xCountSize = 10;
    private Context mContext;

    public DeviceInfoReportViewBinder(Context mContext) {
        this.mContext = mContext;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_dev_detail_chart, parent, false);
        return new ViewHolder(root);
    }


    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull final DevLineData devLineData) {
        holder.chart.setData(devLineData.getLineData());
        initLineChart(holder.chart);
        int pt = devLineData.getPt();
        holder.tvName.setText(TextUtils.isEmpty(PrUtils.getUnitByPt(pt)) ? PrUtils.getNameByPt(pt)
                : PrUtils.getNameByPt(pt) + "    (" + PrUtils.getUnitByPt(pt) + ")");
        holder.chart.getXAxis().setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                int value1 = (int) value;
                if (devLineData.getxValueDateList().size() > value1) {
                    Long times = devLineData.getxValueDateList().get(value1);
                    String formatDate = DateUtils.getMMDDHHMM(times * 1000L);
                    return formatDate;
                } else {
                    return "";
                }
            }
        });
        holder.chart.getLegend().setCustom(devLineData.getLegendEntryList());
        holder.chart.invalidate();

    }




    private void initLineChart(LineChart lineChart) {
        lineChart.getDescription().setEnabled(false);
        lineChart.setDrawGridBackground(true);
        lineChart.setGridBackgroundColor(mContext.getResources().getColor(R.color.backgroud_2));//backgroud_2,color_line_chart_bg

        //不允许缩放
        lineChart.setDoubleTapToZoomEnabled(false);
        lineChart.setPinchZoom(false);
        lineChart.setScaleEnabled(false);

        //设置点击选中后的提示框
        LineMarkerView markerView = new LineMarkerView(mContext, R.layout.view_line_chart_marker);
        markerView.setChartView(lineChart);
        lineChart.setMarker(markerView);

        XAxis xAxis = lineChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setLabelRotationAngle(-60.0f);
        xAxis.setTextColor(mContext.getResources().getColor(R.color.font_gray));
        xAxis.setDrawAxisLine(false);
        xAxis.setLabelCount(xCountSize);
        xAxis.setDrawGridLines(true);
//        xAxis.setAxisLineWidth(1.0f);
        xAxis.setEnabled(true);
        xAxis.setGridColor(mContext.getResources().getColor(R.color.color_line_chart_bg));





        LimitLine limitLine = new LimitLine(0f);
        limitLine.setLineColor(mContext.getResources().getColor(R.color.transparent));//transparent

        YAxis leftAxis = lineChart.getAxisLeft();
        leftAxis.setLabelCount(4, false);
//        leftAxis.setAxisMinimum(-5f); // this replaces setStartAtZero(true)
        leftAxis.setTextColor(mContext.getResources().getColor(R.color.font_gray));
        leftAxis.addLimitLine(limitLine);
//        leftAxis.setEnabled(true); //是否启用X轴
        leftAxis.setDrawAxisLine(false); //是否绘制X轴线
        leftAxis.setDrawGridLines(true);//设置Y轴上每个竖线是否显示
//        leftAxis.setGridLineWidth(1.0f);
        leftAxis.setGridColor(mContext.getResources().getColor(R.color.red_normal));//R.color.color_line_grid     color_line_chart_bg
        leftAxis.enableGridDashedLine(5f, 5f, 0f);
        lineChart.getAxisRight().setEnabled(false);//右边的轴不显示






        int lineChartSize = 0;
        if(mContext instanceof DeviceDetailInfoActivity){
            lineChartSize = ((DeviceDetailInfoActivity)mContext).getLineChartSize();

        }

        Legend legend = lineChart.getLegend(); //拆线图说明
        legend.setForm(Legend.LegendForm.SQUARE);//样式
        legend.setPosition(Legend.LegendPosition.RIGHT_OF_CHART);
         if(lineChartSize > 5){
             legend.setOrientation(Legend.LegendOrientation.VERTICAL);//样式
         }else{
             legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);//样式
         }
        legend.setDirection(Legend.LegendDirection.LEFT_TO_RIGHT);
        legend.setWordWrapEnabled(true);//设置是否换行
        legend.setXEntrySpace(10f);// 设置在水平轴上 legend-entries 的间隙。
        legend.setYEntrySpace(10f);// 设置在垂直轴上 legend-entries 的间隙。
//        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
//        legend.setDirection(Legend.LegendDirection.LEFT_TO_RIGHT);//设置比例图标和文字之间的位置方向
        legend.setTextColor(mContext.getResources().getColor(R.color.font_gray));
//
        /**设置缩放*/
        //设置chart是否可以触摸
        lineChart.setTouchEnabled(true);
        //设置是否可以拖拽
        lineChart.setDragEnabled(true);
        //设置是否可以缩放 x和y，默认true
        lineChart.setScaleEnabled(true);
        //是否缩放X轴
        lineChart.setScaleXEnabled(true);
        //X轴缩放比例
        lineChart.setScaleX(1.0f);
        //Y轴缩放比例
        lineChart.setScaleY(1.0f);
        //是否缩放Y轴
        lineChart.setScaleYEnabled(false);
        //设置是否可以通过双击屏幕放大图表。默认是true
        lineChart.setDoubleTapToZoomEnabled(true);


    }



    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvName;
        LineChart chart;
//        LineChartView lineChartView;

        ViewHolder(View itemView) {
            super(itemView);
            chart = (LineChart) itemView.findViewById(R.id.dev_line_chart);
//            lineChartView = (LineChartView) itemView.findViewById(R.id.dev_hello_line_chart);
            tvName = (TextView) itemView.findViewById(R.id.text_name);

        }

    }
}

